package com.easy.component.ibase;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.component.subscription.EasyBaseSubscription;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 描述: {描述}
 * 作者:jack-song
 * 创建日期: 2017/11/2 10:34
 * 修改日期: 2017/11/2
 * 版本: {版本}
 */
public abstract class EasyBaseFragment extends Fragment implements EasyBaseFramentController {
    protected View containerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().register(this);
        containerView = initlayoutResID(inflater);
        init();
        if (null != containerView) {
            return containerView;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    protected void init() {
        initBundle();
        initView();
        initHeader();
        initLoadingView();
        initData();
    }

    /**
     * 设置头部沉浸式状态栏
     *
     * @param statusBarColor 状态栏颜色 如不设置传0 默认为页面背景色，
     * @param isDarkTheme    设置字体是否为深色
     * @param isStatusPic    设置布局是否顶进状态栏 （一般为图片时使用）
     *                       （如图片不需进入状态栏 且仍要沉浸时，要在布局中加入StatusBarHeightView）
     */
    protected void setStatusTheme(int statusBarColor, boolean isDarkTheme, boolean isStatusPic) {
        getEasyBaseActivity().setStatusTheme(statusBarColor, isDarkTheme, isStatusPic);
    }


    protected void goActivity(Class<?> cls) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().goActivity(cls);
        }

    }

    protected void goActivity(Class<?> cls, Bundle bundle) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().goActivity(cls, bundle);
        }
    }

    protected void goActivityForResult(Class<?> cls, int requestCode) {
        this.goActivityForResult(cls, (Bundle) null, requestCode);
    }

    protected void goActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().goActivityForResult(cls, bundle, requestCode);
        }

    }

    public void finishActivity() {
        getActivity().finish();
    }

    /**
     * 显示loading
     * dialog 可以取消
     * activity不跟随返回键关闭
     */
    public void showLoading() {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoading();
        }
    }

    /**
     * 显示loading
     * dialog 可以取消
     * activity不跟随返回键关闭
     *
     * @param message
     */
    public void showLoading(CharSequence message) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoading(message);
        }
    }

    /**
     * 显示loading
     *
     * @param isCancelable 是否可以取消
     */
    public void showLoading(boolean isCancelable) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoading(isCancelable);
        }
    }

    /**
     * 显示loading
     * dialog 不可以取消
     *
     * @param message
     */
    public void showLoading(CharSequence message, boolean isCancelable) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoading(message, isCancelable);
        }
    }

    /**
     * 显示loading
     * dialog 不可以取消
     */
    public void showLoadingCloseActivity() {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoadingCloseActivity();
        }
    }

    /**
     * @param message
     * @param
     */
    public void showLoadingCloseActivity(CharSequence message) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoadingCloseActivity(message);
        }
    }

    /**
     * 显示loading
     *
     * @param message
     * @param isCancelable
     * @param isCloseActivity
     */
    public void showLoading(CharSequence message, boolean isCancelable, boolean isCloseActivity) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showLoading(message, isCancelable, isCloseActivity);
        }
    }
    /**
     * 隐藏loading
     */
    public void hideLoading() {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().hideLoading();
        }
    }

    /**
     * @param title
     * @param message
     * @param leftOnClickListener
     */
    public void showAlert(CharSequence title, CharSequence message, View.OnClickListener leftOnClickListener) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showAlert(title, message, leftOnClickListener);
        }
    }

    /**
     * 显示alert 样式的弹出框
     *
     * @param message
     * @param leftOnClickListener
     * @param rigthOnClickListener
     */
    public void showAlert(CharSequence message, View.OnClickListener leftOnClickListener, View.OnClickListener rigthOnClickListener) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showAlert(message, leftOnClickListener, rigthOnClickListener);
        }
    }

    /**
     * @param title
     * @param message
     * @param leftOnClickListener
     * @param rigthOnClickListener
     */
    public void showAlert(CharSequence title, CharSequence message, View.OnClickListener leftOnClickListener, View.OnClickListener rigthOnClickListener) {
        if (null != getEasyBaseActivity()) {
            getEasyBaseActivity().showAlert(title, message, leftOnClickListener, rigthOnClickListener);
        }
    }

    public EasyBaseActivity getEasyBaseActivity() {
        return (EasyBaseActivity) getActivity();
    }

    @Subscribe
    public void onEventMainThread(EasyBaseSubscription event) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
